import {urls} from "../../utils/urls"

const app = getApp()
let _this;
Page({
  data: {
    phone:'',
    captcha:'',
  },
  ddinput(e){
    let name = e.currentTarget.dataset.name;
    // console.log(name);
    this.data[name] = e.detail.value;
    this.setData({
      phone: this.data.phone,
      captcha: this.data.captcha
    })
  },
  authorLogin(e){
    if(this.data.phone == ''){
      wx.showToast({
        title: '请填写手机号',
        icon:'none'
      })
    }else if(this.data.captcha == ''){
      wx.showToast({
        title: '请填写验证码',
        icon:'none'
      })
    }else{
      wx.showLoading({
        title: '登录中',
        mask:true
      })
      let userInfo = {"school":wx.getStorageSync('school').id}
      userInfo.username =this.data.phone
      userInfo.captcha = this.data.captcha
      console.log(userInfo)
      app.http('POST',urls.login,userInfo, function (res) {
        wx.hideLoading()
        if(res.code == 100){
          wx.showToast({
            title: '登录成功',
            mask:true,
            duration:800
          })
          let user = res.results
          let token = res.token
          wx.setStorageSync("user",user)
          wx.setStorageSync("token",token)
          setTimeout(
            function(){
            wx.navigateBack({
              detal:1
            })
          },900)
        }else{
          wx.showToast({
            title: '登录失败',
            icon:'none'
          })
        }
      })
    }
  },
  ToCommonLogin(){
    wx.navigateTo({
      url: 'commonlogin/commonlogin',
    })
  },
  sendmessage(){
    if(this.data.phone){
      app.http('GET',urls.sendmessage,{mobile:this.data.phone},function(res){
        wx.showToast({
          title: res.msg,
        })
      })
    }
  },
  checkphone(e){
    if(this.data.phone){
      app.http(
        'GET', // methods
        urls.checkphone, 
        {mobile:this.data.phone}, // data
        function(res){
          console.log(res.results,typeof res.results);
          if(res.results){
            wx.showToast({
              title: '手机号已注册',
              icon:"success"
            })
          }else{
            wx.showToast({
              title: '手机号未注册',
              icon:"error"
            })
          }
        } // success
        )
    }
  }
})